<template>
  <!--评论列表-->
  <div class="comment-body">
    <!--评论内容-->
    <div class="comment">
      <!--父评论内容--用户名 头像 内容-->
      <span class="anchor"></span>
      <div class="image-avatar">
        <img
          :src="'http://chm.pengdong.top/index/get/'+comment.sculpture"
          style="width: 40px; border-radius: 50%"
        />
      </div>
      <div class="content">
        <a class="nickname" target="_blank">{{ comment.nickname }}</a>
        <span class="date">{{ comment.comment.createDate | format }}</span>
        <el-button
          size="mini"
          type="primary"
          @click="setReply(comment.comment.commentId, comment.nickname)"
          >回复</el-button
        >
        <div class="text" v-html="comment.comment.commentContext"></div>
      </div>
      <!--子评论-->
      <div v-if="comment.children.length > 0" class="comments">
        <div
          v-for="(reply,index) in comment.children"
          :key="index"
          class="comment"
        >
          <span class="anchor"></span>
          <div class="image-avatar">
            <img
              :src="'http://chm.pengdong.top/index/get/'+reply.sculpture"
              style="width: 40px; border-radius: 50%"
            />
          </div>
          <div class="content">
            <a class="nickname" rel="external nofollow noopener">{{
              reply.nickname
            }}</a>

            <span class="date">{{ reply.comment.createDate | format }}</span>
            <div class="text">
              <a
                style="
                  text-decoration-line: none;
                  margin-right: 8px;
                  font-weight: bold;
                  color: #333333;
                "
                >@{{ reply.fathernickname }}</a
              >
              <div
                style="display: inline"
                v-html="reply.comment.commentContext"
              ></div>
            </div>
            <div class="actions">
              <el-button
                size="mini"
                type="primary"
                @click="
                  setChildrenReply(
                    reply.comment.commentId,
                    reply.nickname,
                    index
                  )
                "
                >回复</el-button
              >
            </div>
          </div>

          <div class="form" v-show="showCommentEdit1&&(index==nowIndex)">
            <h3>
              发表评论
              <el-button
                class="m-small"
                size="mini"
                type="primary"
                @click="changeEdit1()"
                >取消回复
              </el-button>
            </h3>
            <el-form ref="formRef" :model="comment1" size="small">
              <el-input
                v-model="comment1.commentContext"
                :class="'textarea'"
                :rows="5"
                :validate-event="false"
                maxlength="250"
                placeholder="评论千万条，友善第一条"
                show-word-limit
                type="textarea"
              ></el-input>

              <el-row :gutter="20">
                <el-col :span="6">
                  <h4 style="margin-top:3px">你 回复给 {{ nickname2 }}:</h4>
                </el-col>

                <el-col :span="3">
                    <el-button size="medium" type="primary" @click="beforePost1"
                      >发表评论</el-button
                    >
                </el-col>
              </el-row>
            </el-form>
          </div>
        </div>
      </div>

      <div class="form" v-show="showCommentEdit2">
        <h3>
          发表评论
          <el-button
            class="m-small"
            size="mini"
            type="primary"
            @click="changeEdit2()"
            >取消回复
          </el-button>
        </h3>
        <el-form ref="formRef" :model="comment2" size="small">
          <el-input
            v-model="comment2.commentContext"
            :class="'textarea'"
            :rows="5"
            :validate-event="false"
            maxlength="250"
            placeholder="评论千万条，友善第一条"
            show-word-limit
            type="textarea"
          ></el-input>

          <el-row :gutter="20">
            <el-col :span="5">
              <h4 style="margin-top: 3px">你 回复给 {{ nickname1 }}:</h4>
            </el-col>

            <el-col :span="3">
              <el-button size="medium" type="primary" @click="beforePost2"
                >发表评论</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Comment",
  props: {
    comment: Object,
  },
  data() {
    return {
      nowIndex:"",
      commentCount: 0,
      comments: [],
      nowUser:"",
      noUser:"99999999999",
      parentCommentId: -1,
      blogId: 0,
      showCommentEdit1: false,
      showCommentEdit2: false,
      nickname1: "",
      nickname2: "",
      comment1: {
        commentContext: "",
        parentId: 0,
        articleId: this.$route.params.id,
        userId: localStorage.getItem("uid"),
        commentState: 1,
        nickname: localStorage.getItem("nickname"),
        sculpture: localStorage.getItem("avatar"),
      },
      comment2: {
        commentContext: "",
        parentId: 0,
        articleId: this.$route.params.id,
        userId: localStorage.getItem("uid"),
        commentState: 1,
        nickname: localStorage.getItem("nickname"),
        sculpture: localStorage.getItem("avatar"),
      },
    };
  },
  created(){
    this.nowUser = localStorage.getItem("account")
  },
  methods: {
    //设置回复的父亲id
    setReply(id, nickname) {
      this.comment2.parentId = id;
      this.showCommentEdit2 = true;
      this.nickname1 = nickname;
    },
    //设置回复的父亲id
    setChildrenReply(id, nickname,index) {
      this.comment1.parentId = id;
      this.showCommentEdit1 = id;
      this.nickname2 = nickname;
      this.nowIndex = index;
    },

    changeEdit1() {
      this.showCommentEdit1 = false;
    },
    changeEdit2() {
      this.showCommentEdit2 = false;
    },
    beforePost1() {
      var that = this;
      this.axios({
        method: "post",
        url: "http://chm.pengdong.top/article/publish-comment",
        data: this.comment1,
        headers: {
          // ContentType: "application/json",
          token: localStorage.getItem("token"),
        },
      }).then((res) => {
        var vo = res.data;
        if (vo.code == 10000) {
          console.log("success");
          that.$message({
            message: "发布成功！",
            type: "success",
            showClose: true,
          });
          location.reload();
          console.log(vo);
        } else if (vo.code == 1000) {
          that.$message({
            message: vo.msg,
            type: "warn",
            showClose: true,
          });
        } else {
          console.log("11");
        }
      });
    },
    beforePost2() {
      var that = this;
      this.axios({
        method: "post",
        url: "http://chm.pengdong.top/article/publish-comment",
        data: this.comment2,
        headers: {
          // ContentType: "application/json",
          token: localStorage.getItem("token"),
        },
      }).then((res) => {
        var vo = res.data;
        if (vo.code == 10000) {
          console.log("success");
          that.$message({
            message: "发布成功！",
            type: "success",
            showClose: true,
          });
          location.reload();
          console.log(vo);
        } else if (vo.code == 20002) {
          console.log("登陆过期");
        } else {
          console.log("11");
        }
      });
    },
  },
};
</script>

<style scoped>
.comment-body {
  padding: 5px 35px 15px 22px;
}
.comment {
  padding-right: 2em !important;
  padding-left: 1em !important;
  margin: 7px 0px 0px;
  padding-top: 7px;
}
.comments {
  padding: 42px 0px 24px 31.5px;
  margin: -28px 0px -10px 17.5px;
  border-left: 1px #ccc solid;
}
.content {
  margin-left: 0px;
  display: inline-block;
  width: auto;
  vertical-align: top;
}
.image-avatar {
  display: inline-block;
  width: 49px;
  vertical-align: top;
}
.label {
  margin-left: 5px;
}
.nickname {
  font-weight: bolder;
  font-size: 14px;
  color: #000;
  text-decoration-line: none;
}
.comment .el-button {
  margin-left: 5px;
  padding: 4px 5px;
}
.text {
  white-space: pre-wrap !important;
  margin: 7px 0px;
  line-height: 1.5;
  font-size: 14px;
}
.date {
  color: gray;
  font-size: 10px;
  margin-left: 5px;
}
.anchor {
  height: 55px;
  margin-top: -55px;
  visibility: hidden;
}

.el-button--primary {
  color: white;
}
</style>